package com.yndsoft.jc.ui.xiaoyao;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.yndsoft.jc.R;

import in.srain.cube.views.loadmore.LoadMoreContainer;
import in.srain.cube.views.loadmore.LoadMoreUIHandler;

/**
 * Created by Administrator on 2016-08-11.
 * 参考：http://blog.csdn.net/jan_s/article/details/50663798
 */
public class XYLoadMoreFooterView extends RelativeLayout implements LoadMoreUIHandler {
    private ImageView loadingImage;
    private TextView loadText;

    public XYLoadMoreFooterView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public XYLoadMoreFooterView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setupViews();
    }

    public XYLoadMoreFooterView(Context context) {
        this(context, null);
    }

    private void setupViews() {
        LayoutInflater.from(getContext()).inflate(R.layout.loadmore_footer, this);
        loadingImage = (ImageView) findViewById(R.id.loadmore_ico);
        loadText = (TextView) findViewById(R.id.loadmore_title);
        setVisibility(GONE);
    }

    // 加载中
    @Override
    public void onLoading(LoadMoreContainer container) {
        setVisibility(VISIBLE);
        loadingImage.setVisibility(VISIBLE);
        loadText.setText(R.string.loading);
        final RotateAnimation anim = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        anim.setInterpolator(new LinearInterpolator());
        anim.setRepeatCount(-1);
        anim.setDuration(1000);
        //loadingImage.setImageResource(R.drawable.progress_loading2);
        loadingImage.setImageResource(R.drawable.loading);
        loadingImage.startAnimation(anim);
    }

    // 加载完成: 数据为空 / 没有更多
    @Override
    public void onLoadFinish(LoadMoreContainer container, boolean empty, boolean hasMore) {
        if (!hasMore) {
            setVisibility(GONE);
            if (empty) {
                loadText.setText(R.string.loading_finish);
            } else {
                loadText.setText(R.string.loading_finish_empty);
            }
            //Toast.makeText(getContext(), R.string.loading_finish_empty, Toast.LENGTH_SHORT).show();
            //loadText.setVisibility(GONE);
            //loadingImage.setVisibility(GONE);
        } else {
            setVisibility(INVISIBLE);
        }
    }

    // 等待加载更多
    @Override
    public void onWaitToLoadMore(LoadMoreContainer container) {
        setVisibility(VISIBLE);
        loadText.setText(R.string.loading_click_more);
        loadingImage.setVisibility(INVISIBLE);
    }

    // 加载失败
//    @Override
//    public void onLoadError(LoadMoreContainer container, int errorCode, String errorMessage) {
//        loadingImage.clearAnimation();
//        loadingImage.setImageResource(R.drawable.ic_launcher);
//        loadText.setText(R.string.loading_error);
//    }
}
